package wang.zl.bookmanager1.R;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;


/**
 * @author like 全局异常处理
 * @date 2022/9/27 6:22
 * @Description TODO
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResultInfo exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.error(ex.getMessage());
        if(ex.getMessage().contains("Duplicate")){
            String [] split = ex.getMessage().split(" ");
            String msg = split[2] + "已经重复了";
            return new ResultInfo(false,null,msg);
        }
        return new ResultInfo(false,null,"操作失败");
    }

    }
